package com.estate.service.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.estate.bean.Complaint;
import com.estate.bean.Owner;
import com.estate.service.service.ComplaintService;
import com.estate.util.JsonResult;
import com.estate.util.ResultTool;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/complaint")
public class ComplaintController {

    @Resource
    private ComplaintService complaintService;

    @GetMapping("/list/{page}/{size}")
    JsonResult list(@PathVariable("page") int page, @PathVariable("size") int size) {
        log.info("page:{},size:{}", page, size);
        JsonResult result = complaintService.listByPage(page, size);
        log.info("result:{}", result);
        return result;
    }

    @PostMapping("/save")
    JsonResult save(Complaint complaint) {
        log.info("新增complaint{}", complaint);
        JsonResult result = complaintService.saveComplaint(complaint);
        log.info("result:{}", result);
        return result;
    }

    @DeleteMapping("/delete")
    JsonResult delete(@RequestParam("complaintId") String complaintId) {
        log.info("删除的complaintId{}", complaintId);
        JsonResult result = complaintService.deleteComplaint(complaintId);
        log.info("result:{}", result);
        return result;
    }

    @PutMapping("/update")
    JsonResult update(Complaint complaint) {
        log.info("修改complaint{}", complaint);
        JsonResult result = complaintService.updateComplaint(complaint);
        log.info("result:{}", result);
        return result;
    }

    @GetMapping("/list/{complaintId}")
    public JsonResult list(@PathVariable("complaintId") String complaintId){
        log.info("complaintId:{}", complaintId);
        JsonResult result = ResultTool.success(complaintService.getById(complaintId));
        log.info("result:{}", result);
        return result;
    }

    @GetMapping("/findByCommunityId/{communityId}")
    public Long findByCommunityId(@PathVariable("communityId") String communityId) {
        log.info("communityId:{}", communityId);
        QueryWrapper<Complaint> wrapper=new QueryWrapper();
        wrapper.eq("community_id",communityId);
        long count = complaintService.count(wrapper);
        log.info("result:{}", count);
        return count;
    }
}
